package Shuffle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 玩家資訊，會有姓名，個人牌組資訊
 * @author QK
 */
public class Player {
	
	/**
	 * 玩家姓名
	 */
	private String name;
	
	/**
	 * 玩家出牌順序
	 */
	private int playerOrder;
	
	/**
	 * 玩家人數
	 */
	private int pleyerNumber;
	
	/**
	 * 每位玩家分配的牌,Key=玩家姓名,value該玩家的牌組
	 */
	private Map <Player,List<Poke>> playerCards[]; 
	
	/**
	 * 要把玩家放進去
	 */
	private Player[] player;
	
	
	/**
	 * 預設實際發排順序
	 */
	private int defaultPlayerOrder[] = {0,1,2,3};
	
	/**
	 * 個人自己的牌組
	 */
	private List<Poke> listPoke;
	
	public Player(){
	}
	
	public void setPlayer(String name, int playerOrder){
		this.name = name;
		this.playerOrder = playerOrder;
	}
	
	public int getPleyerNumber() {
		return pleyerNumber;
	}
	
	public Player[] getPlayers() {
		return player;
	}

	/**
	 * 設定玩家人數，最低人數2人，最多4人，目前不支援3人
	 * @param pleyer
	 */
	public void setPleyer(int pleyer) {
		if(pleyer <2){
			this.pleyerNumber = 2;
		}else{
			this.pleyerNumber = pleyer;
		}
//		System.out.println("玩家人數="+pleyerNumber);
	}
	
	/**
	 * 測試使用者資料
	 */
	public void setPlayerInfo(){
		String names[] ={"Lorn","Runnie","AnnSun","Rabbit"};
		player = new Player[names.length];
		for(int nums = 0 ; nums < names.length ;nums++){
			player[nums] = new Player();
			player[nums].setName(names[nums]);
			player[nums].setPlayerOrder(defaultPlayerOrder[nums]);
//			System.out.println(player[nums].getName()+"-"+player[nums].getPlayerOrder());
		}
	}
	
	/**
	 * 洗牌後，要把牌分給玩家
	 * @param cards 需要傳入pokeTypeB整張牌組
	 */
	public void setPlayerCard(Map <String, Poke> cards){
		int playNum = 0;					//第幾個玩家的牌
		int dealCard = 52 / pleyerNumber;	//分配牌數
		playerCards = new HashMap [pleyerNumber];
		listPoke = new ArrayList<Poke>();
		for(int count = 1 ; count < 53 ; count++){
			Poke aCard = (Poke) cards.get(String.valueOf(count));
			listPoke.add(aCard);
			if(count % dealCard ==0){
				playerCards[playNum] = new HashMap< Player, List<Poke>>();
//				playerCards[playNum].put(player[playNum], new Poke().cardSort(pokeList));	//幫他排序這副牌
				player[playNum].setListPoke(listPoke);
				playerCards[playNum].put(player[playNum], listPoke);
				listPoke = new ArrayList<Poke>();
				playNum++;
			}
		}
	}
	
	/**
	 * 分好牌後，要重新排序
	 * @param cards 需要傳入pokeTypeB整張牌組
	 */
	public void reSetPlayerCard(Player player){
		Player[] players = player.getPlayers();		//取得所有玩家
		for(int i = 0 ; i < pleyerNumber ; i++){
			List<Poke> pokeList = new Poke().cardSort(playerCards[i].get(players[i]));
			playerCards[i].remove(players[i]);
			playerCards[i].put(players[i], pokeList);
		}
	}

	/**
	 * 秀該出所有玩家的牌
	 */
	public void showPlayerCards(){
		for(int nums = 0 ; nums < pleyerNumber ; nums++){
			String who = player[nums].getName();
			System.out.print(who+"-");
			List<Poke> pokeList = playerCards[nums].get(player[nums]);
//			pokeList.forEach(Poke -> System.out.print(Poke.color+Poke.number+"\t"));
			for(int cards = 0 ; cards < pokeList.size() ; cards++){
				Poke aCard= (Poke)pokeList.get(cards);
				System.out.print(aCard.color+aCard.number+"\t");
			}
			System.out.println();
		}
	}
	
	
	/**
	 * 傳入玩家所有的牌，然後替玩家由小到大排序
	 * @param player 要傳入Player的牌組
	 */
	public void showPlayerOrderCards(Player player){
		
		Map<Player, List<Poke>> myCard[] = player.getPlayerCards();
		Player[] pp = player.getPlayers();			//取得所有玩家
		//4種花色
		for(int i = 0 ; i < 4 ; i ++){		
			System.out.print(pp[i].getName()+"-");
			List<Poke> listPoke= myCard[i].get(pp[i]);	//取得該玩家的牌
			listPoke = new Poke().cardSort(listPoke);			
//			listPoke.forEach(Poke -> System.out.print(Poke.color+Poke.number+"\t"));
			for(int j = 0 ; j<  listPoke.size() ; j++){	//印出該牌組
				Poke a = listPoke.get(j);
				System.out.print(a.color+a.number+"\t");
			}
			System.out.println();
		}
	}
	
	public List<Poke> getPersonCard(int index){
		return playerCards[index].get(player[index]);
	}
	
	public Map<Player, List<Poke>>[] getPlayerCards(){
		return playerCards;
	}
	
	public void setPlayerCards(Map<Player, List<Poke>>[] cards){
		this.playerCards = cards;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPlayerOrder() {
		return playerOrder;
	}

	public void setPlayerOrder(int playerOrder) {
		this.playerOrder = playerOrder;
	}

	/**
	 * 取得該位玩家排組
	 * @param index 第幾位玩家
	 * @return 回傳listPoke
	 */
	public List<Poke> getListPoke(int index) {
		return listPoke;
	}

	/**
	 * 儲存該單位玩家自己的排組
	 * @param listPoke
	 */
	public void setListPoke(List<Poke> listPoke) {
		this.listPoke = listPoke;
	}
}
